﻿#region Header
// ------------------------ Licence / Copyright ------------------------
// 
// ScrumTable for Scrum Meeting Support
// Copyright © HSR - Hochschule für Technik Rapperswil
// All Rights Reserved
// 
// Author:
//  Michael Gfeller Silvan Gehrig Patrick Boos
// 
// ---------------------------------------------------------------------
#endregion

#region Usings

using System;
using System.Collections.Generic;

using ScrumTable.Common;
using ScrumTable.Common.Logging;
using ScrumTable.DL.Data.Common;
using ScrumTable.DL.Data.Properties;

#endregion

namespace ScrumTable.DL.Data
{
    /// <summary>
    /// The DataConnectionFactory returns a new IDataContext instance
    /// associated to the given connection type. You may specifiy a
    /// separate assembly name (e.g. MyCompany.Connectors.DataProvider.dll).
    /// This assembly must contain a DataAssemblyAttribute, which specifies
    /// the provider entry point.
    /// </summary>
    /// <remarks>The implementation follows the Simple Factory Method pattern.</remarks>
    public class DataConnectionFactory
    {
        #region Declarations
        //--------------------------------------------------------------------
        // Declarations
        //--------------------------------------------------------------------

        private static IList<DataAssembly> _dataAssemblies;

        #endregion

        #region Properties
        //--------------------------------------------------------------------
        // Properties
        //--------------------------------------------------------------------

        /// <summary>
        /// Gets a list of available data assemblies. This will load the
        /// assemblies from the file system.
        /// </summary>
        public static IList<DataAssembly> AvailableDataAssemblies
        {
            get { return DataAssembly.FromDriversDirectory(Environment.CurrentDirectory); }
        }

        /// <summary>
        /// Gets a list of available data assemblies. This will load the
        /// assemblies from the file system.
        /// </summary>
        /// <remarks>
        /// Caution:
        /// The use of this property IS NOT MULTI-THREADING SAVE!
        /// DO NOT CHANGE ANY DATA OUTSIDE FROM THE GUI THREAD!
        /// </remarks>
        public static IList<DataAssembly> CachedDataAssemblies
        {
            get
            {
                if (_dataAssemblies == null)
                {
                    _dataAssemblies = AvailableDataAssemblies;
                }
                return _dataAssemblies;
            }
        }

        #endregion

        #region Constructors / Destructor
        //--------------------------------------------------------------------
        // Constructors / Destructor
        //--------------------------------------------------------------------

        #endregion

        #region Methods
        //--------------------------------------------------------------------
        // Methods
        //--------------------------------------------------------------------

        /// <summary>
        /// Creates a new data connection which is stored in the given assembly.
        /// </summary>
        /// <param name="assembly">Assembly file name to retrieve. This assembly must contain a DataAssembly attribute.</param>
        /// <returns>Returns the retrieved connection.</returns>
        public static IDataContext Create(DataAssembly assembly)
        {
            PreCondition.AssertNotNull(assembly, "assembly");

            Logger<DataConnectionFactory>.InfoFormat(Resources.LogCreateDataContextAssembly, assembly);
            return assembly.InstantiateDataContext();
        }

        #endregion

        #region Events
        //--------------------------------------------------------------------
        // Events
        //--------------------------------------------------------------------

        #endregion
    }
}